<template>
  <div class="register-wrapper">
    <div class="register-container">
      <div class="form-header">
        <h2>创建账号</h2>
        <p>开启您的美好旅程</p>
      </div>
      <form @submit.prevent="handleRegister" class="floating-form">
        <div class="input-group">
          <input type="text" id="username" v-model="username" required maxlength="20" />
          <label for="username">用户名</label>
          <span class="highlight"></span>
        </div>


        <div class="input-group">
          <input type="password" id="password" v-model="password" required minlength="6" maxlength="20" />
          <label for="password">密码(至少6位）</label>
          <span class="highlight"></span>
        </div>
        <div class="input-group">
          <input type="password" id="confirmPassword" v-model="rePassword" required minlength="6" maxlength="20" />
          <label for="confirmPassword">确认密码</label>
          <span class="highlight"></span>
        </div>

          <div v-if="errorMessage" class="error-message">
            <i class="icon-alert"></i>
            {{ errorMessage }}
          </div>
        <div v-if="successMessage" class="success-message">
        <i class="icon-success"></i>
        {{ successMessage }}
      </div>
        <button type="submit" class="submit-btn">
          <span>立即注册</span>
          <i class="arrow-icon"></i>
        </button>
        <div class="form-footer">
          <span>已有账号？</span>
          <a href="/login">立即登录</a>
        </div>
      </form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      username: '',
      password: '',
      rePassword: '',
      errorMessage: '',
      successMessage: ''
    }
  },
  methods: {
    async handleRegister() {
      if (this.password !== this.rePassword) {
        this.errorMessage = '两次密码不一致';
        return;
      }

      const ws = new WebSocket('ws://localhost:8080');
      const timeout = setTimeout(() => {
        ws.close();
        this.errorMessage = '连接超时，请重试';
      }, 5000);

      ws.onopen = () => {
        ws.send(JSON.stringify({
          messageType: 2,
          username: this.username,
          password: this.password,
          rePassword: this.rePassword
        }));
      };

      ws.onmessage = (event) => {
        clearTimeout(timeout);
        try {
          const response = JSON.parse(event.data);
          if (response.success) {
            this.successMessage = '注册成功！即将跳转登录页面...';
            this.errorMessage = '';
            setTimeout(() => {
              this.$router.push('/login');
            }, 2000);
          } else {
            this.errorMessage = response.reason || '注册失败';
          }
        } catch (e) {
          this.errorMessage = '服务器返回数据格式错误';
        } finally {
          ws.close();
        }
      };

      ws.onerror = (error) => {
        clearTimeout(timeout);
        this.errorMessage = '连接异常，请检查网络';
        ws.close();
      };
    }
  }
}
</script>

<style scoped>
.success-message {
  background: #f0fff4;
  color: #2f855a;
  padding: 12px;
  border-radius: 4px;
  margin-bottom: 25px;
  display: flex;
  align-items: center;
}
.error-message {
  background: #fff0f0;
  color: #d8000c;
  padding: 12px;
  border-radius: 4px;
  margin-bottom: 25px;
  display: flex;
  align-items: center;
}
.register-wrapper {
  min-height: 100vh;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  background-image: url("src/assets/re_bg.png");
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  position: absolute;
  top: 0;
  left: 0;
}



html, body {
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
  overflow: hidden;
}
.register-container {
  width: 100%;
  max-width: 480px;
  background: white;
  border-radius: 20px;
  padding: 40px;
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
}

.form-header {
  text-align: center;
  margin-bottom: 40px;
}

.form-header h2 {
  color: #2c3e50;
  font-size: 32px;
  margin-bottom: 10px;
  font-weight: 700;
}

.form-header p {
  color: #95a5a6;
  font-size: 16px;
}

.floating-form .input-group {
  position: relative;
  margin-bottom: 30px;
}

.input-group input {
  width: 100%;
  padding: 15px;
  border: 2px solid #e0e0e0;
  border-radius: 12px;
  font-size: 16px;
  transition: all 0.3s ease;
  background: transparent;
}

.input-group label {
  position: absolute;
  left: 15px;
  top: 50%;
  transform: translateY(-50%);
  background: white;
  padding: 0 5px;
  color: #95a5a6;
  font-size: 16px;
  transition: all 0.3s ease;
  pointer-events: none;
}

.input-group input:focus,
.input-group input:valid {
  border-color: #34db63;
}

.input-group input:focus + label,
.input-group input:valid + label {
  top: 0;
  font-size: 14px;
  color: #53db34;
}


.verification-group input {
  flex: 1;
}

.submit-btn {
  width: 100%;
  padding: 15px;
  background: linear-gradient(to right, #34db63, #2eb929);
  color: white;
  border: none;
  border-radius: 12px;
  font-size: 18px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
}

.submit-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 5px 15px rgba(52, 152, 219, 0.3);
}

.arrow-icon {
  border: solid white;
  border-width: 0 2px 2px 0;
  display: inline-block;
  padding: 3px;
  transform: rotate(-45deg);
}

.form-footer {
  text-align: center;
  margin-top: 20px;
  color: #95a5a6;
}

.form-footer a {
  color: #53db34;
  text-decoration: none;
  margin-left: 5px;
  font-weight: 600;
}

.form-footer a:hover {
  text-decoration: underline;
}

@media (max-width: 480px) {
  .register-container {
    padding: 20px;
  }

  .form-header h2 {
    font-size: 24px;
  }

  .input-group input {
    padding: 12px;
  }
}

@media (max-width: 768px) {
  .register-container {
    max-width: 400px;
    padding: 30px;
  }

  .form-header h2 {
    font-size: 28px;
  }

  .form-header p {
    font-size: 14px;
  }
}

@media (max-width: 480px) {
  .register-container {
    padding: 20px;
    margin: 10px;
    max-width: 100%;
  }

  .form-header h2 {
    font-size: 24px;
  }

  .form-header p {
    font-size: 14px;
  }

  .input-group input {
    padding: 12px;
    font-size: 14px;
  }

  .input-group label {
    font-size: 14px;
  }

  .submit-btn {
    padding: 12px;
    font-size: 16px;
  }
}

@media (max-width: 320px) {
  .register-container {
    padding: 15px;
  }

  .form-header h2 {
    font-size: 20px;
  }

  .input-group {
    margin-bottom: 20px;
  }
}
</style>